﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Sinacor.Infra.Tools.ErrorLogCollector
{
    public class EventLogCollector
    {
        public List<EventLogData> Collect(string source, LogSource logSource, DateTime from, DateTime to)
        {
            EventLog log = new EventLog("Application");
            List<EventLogData> logs = new List<EventLogData>();

            foreach (EventLogEntry entry in log.Entries)
            {
                if (entry.EntryType == EventLogEntryType.Error && 
                    entry.Source == source && 
                    entry.TimeGenerated >= from && 
                    entry.TimeGenerated <= to)
                {
                    EventLogData data = new EventLogData();

                    data.LogSource = logSource;
                    data.Date = entry.TimeGenerated;
                    data.Content = entry.Message;

                    data.EventLogSource = entry.Source;
                    data.EventLogCategory = entry.Category ?? string.Empty;
                    data.EventLogType = entry.EntryType;
                    data.EventLogId = entry.InstanceId;
                    data.EventLogUserName = entry.UserName ?? string.Empty;
                    data.EventLogMachineName = entry.MachineName ?? string.Empty;

                    logs.Add(data);
                }
            }

            return logs;
        }
    }
}
